package com.ruoyi.standingbook.sanitationvehicle.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import com.ruoyi.standingbook.sanitationvehicle.domain.SanitationVehicle;
import com.ruoyi.standingbook.sanitationvehicle.domain.SanitationVehicleImportDetail;
import com.ruoyi.standingbook.sanitationvehicle.domain.bo.SanitationVehicleImportDetailBo;
import com.ruoyi.standingbook.sanitationvehicle.domain.vo.SanitationVehicleImportDetailVo;
import com.ruoyi.standingbook.sanitationvehicle.mapper.SanitationVehicleImportDetailMapper;
import com.ruoyi.standingbook.sanitationvehicle.service.ISanitationVehicleImportDetailService;

import java.util.List;
import java.util.Collection;

/**
 * 环卫作业车导入明细Service业务层处理
 *
 * @author lq
 * @date 2024-01-03
 */
@RequiredArgsConstructor
@Service
public class SanitationVehicleImportDetailServiceImpl implements ISanitationVehicleImportDetailService {

    private final SanitationVehicleImportDetailMapper baseMapper;

    /**
     * 查询公园管养导入明细
     */
    @Override
    public SanitationVehicleImportDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询公园管养导入明细列表
     */
    @Override
    public TableDataInfo<SanitationVehicleImportDetailVo> queryPageList(SanitationVehicleImportDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SanitationVehicleImportDetail> lqw = buildQueryWrapper(bo);
        Page<SanitationVehicleImportDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询公园管养导入明细列表
     */
    @Override
    public List<SanitationVehicleImportDetailVo> queryList(SanitationVehicleImportDetailBo bo) {
        LambdaQueryWrapper<SanitationVehicleImportDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SanitationVehicleImportDetail> buildQueryWrapper(SanitationVehicleImportDetailBo bo) {
        LambdaQueryWrapper<SanitationVehicleImportDetail> lqw = Wrappers.lambdaQuery();

		lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()),
				SanitationVehicleImportDetail::getBatchNo, bo.getBatchNo());
		lqw.like(StringUtils.isNotBlank(bo.getProjectName()),
				SanitationVehicleImportDetail::getProjectName, bo.getProjectName());
		lqw.eq(bo.getProcessStatus() != null,
				SanitationVehicleImportDetail::getProcessStatus, bo.getProcessStatus());

		lqw.ge(bo.getCheckDateBegin() != null,
				SanitationVehicleImportDetail::getCheckDate, bo.getCheckDateBegin());
		lqw.le(bo.getCheckDateEnd() != null,
				SanitationVehicleImportDetail::getCheckDate, bo.getCheckDateEnd());

		lqw.orderByDesc(SanitationVehicleImportDetail::getCheckDate);
		
        return lqw;
    }

    /**
     * 新增公园管养导入明细
     */
    @Override
    public Boolean insertByBo(SanitationVehicleImportDetailBo bo) {
    	SanitationVehicleImportDetail add = BeanUtil.toBean(bo, SanitationVehicleImportDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改公园管养导入明细
     */
    @Override
    public Boolean updateByBo(SanitationVehicleImportDetailBo bo) {
    	SanitationVehicleImportDetail update = BeanUtil.toBean(bo, SanitationVehicleImportDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SanitationVehicleImportDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除公园管养导入明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean batchInsertOrUpdate(List<SanitationVehicleImportDetailVo> voList) {
        List<SanitationVehicleImportDetail> list= BeanCopyUtils.copyList(voList , SanitationVehicleImportDetail.class);
        return baseMapper.insertOrUpdateBatch(list);
    }
}
